package com.slipper.groovy.util;

import com.slipper.PaasContext;
import com.slipper.constants.Constants;
import com.slipper.groovy.vo.Message;
import com.slipper.system.codeless.bo.VariableBO;
import com.slipper.system.codeless.service.VariableService;
import com.slipper.system.dictionary.bo.DictionaryDataBO;
import com.slipper.system.dictionary.service.DictionaryDataService;
import com.slipper.system.role.bo.RoleBO;
import com.slipper.util.FileUtil;
import com.slipper.util.MD5;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SystemUtil {
    public static Boolean isCurrentUserAdmin() {
        return Constants.getCurrentUser().isSuperAdmin();
    }

    public static String getCurrentUserId() {
        return Constants.getCurrentUserId();
    }

    public static Map<String, Object> getCurrentUserData() {
        return Constants.getCurrentUser().getUser();
    }

    public static String getCurrentOrgId() {
        return Constants.getCurrentUser().getOrgId();
    }

    public static Map<String, Object> getCurrenOrgData() {
        return Constants.getCurrentUser().getOrg();
    }

    public static String getCurrentUnitId() {
        return Constants.getCurrentUser().getUnitId();
    }

    public static List<RoleBO> getCurrentRoleList() {
        return Constants.getCurrentUser().getRoleList();
    }

    public static String getCaptcha() {
        return Constants.getCaptcha();
    }

    public static String getVariable(String name) {
        VariableBO variable = PaasContext.getBean(VariableService.class).queryByName(name);
        return variable == null ? null : variable.getValue();
    }

    public static int updateVariable(String name, String value) {
        Constants.getCurrentUser();
        VariableBO variable = PaasContext.getBean(VariableService.class).queryByName(name);
        variable.setValue(value);
        return PaasContext.getBean(VariableService.class).save(variable);
    }

    public static List<Map<String, String>> getDictList(String dictName) {
        DictionaryDataBO qo = new DictionaryDataBO();
        qo.setDictName(dictName);
        List<DictionaryDataBO> dataList = PaasContext.getBean(DictionaryDataService.class).queryList(qo);
        List<Map<String, String>> resultList = new ArrayList<>();
        dataList.forEach(data -> {
            Map<String, String> resuleMap = new HashMap<>();
            resuleMap.put("parentKey", data.getDictParent());
            resuleMap.put("key", data.getDictKey());
            resuleMap.put("value", data.getDictValue());
            resuleMap.put("ordinal", String.valueOf(data.getOrdinal()));
            resultList.add(resuleMap);
        });
        return resultList;
    }

    public static HttpServletRequest getCurrentRequest() {
        return Constants.getCurrentRequest();
    }

    public static HttpServletResponse getCurrentResponse() {
        return Constants.getCurrentResponse();
    }

    public static void download(String fileName, String filePath) throws FileNotFoundException {
        download(fileName, new File(filePath));
    }

    public static void download(String fileName, File file) throws FileNotFoundException {
        download(fileName, new FileInputStream(file), file.length());
    }

    public static void download(String fileName, InputStream inputStream, long length) {
        FileUtil.download(getCurrentResponse(), fileName, inputStream, length);
    }

    public static Message success(String message) {
        return new Message(true, "1", message);
    }

    public static Message failure(String message) {
        return new Message(false, "0", message);
    }

    public static Message failure(String code, String message) {
        return new Message(false, code, message);
    }

    public static String encodePwd(String password) {
        return MD5.encodePwd(password);
    }
}
